#include "entRectgrid.h"

#include <assert.h>

#include "types.h"

//--------------------------------------------------------------------------------------------------

EntRectGrid::EntRectGrid()
{
	colour.r = 255.0f;
	colour.g = 255.0f;
	colour.b = 255.0f;
	colour.a = 255.0f;

	type = ENT_TYPE_RECT_GRID;

	gridWidth = 2;
	gridHeight = 2;
}

EntRectGrid::EntRectGrid(ofxXmlSettings &xml) : Entity(xml)
{
	// Load colour values
	colour.r = xml.getValue("colour:r", 255.0f);
	colour.g = xml.getValue("colour:g", 255.0f);
	colour.b = xml.getValue("colour:b", 255.0f);
	colour.a = xml.getValue("colour:a", 255.0f);

	gridWidth = xml.getValue("gridWidth", 2);
	gridHeight = xml.getValue("gridHeight", 2);

	type = ENT_TYPE_RECT_GRID;
}

void EntRectGrid::customDraw()
{
	ofPushStyle();
	ofNoFill();
	ofSetLineWidth(10.0f);
	ofSetColor(colour);
	for(int x = -gridWidth / 2; x < gridWidth / 2; x++)
		for(int y = -gridHeight / 2; y < gridHeight / 2; y++)
			ofRect(1 * x, 1 * y, 0, 1,1);
	ofPopStyle();
}

void EntRectGrid::save(ofxXmlSettings &xml)
{
	// Standard attribute save ** IMPORTANT
	Entity::save(xml);

	// Custom entity attributes
	xml.setValue("colour:r", colour.r);
	xml.setValue("colour:g", colour.g);
	xml.setValue("colour:b", colour.b);
	xml.setValue("colour:a", colour.a);

	xml.setValue("gridWidth", gridWidth);
	xml.setValue("gridHeight", gridHeight);
}

Entity* EntRectGrid::copy()
{
	EntRectGrid* newRectGrid = new EntRectGrid();

	entityStandardAttributesCopy(newRectGrid, this);

	newRectGrid->colour = this->colour;
	newRectGrid->gridWidth = gridWidth;
	newRectGrid->gridHeight = gridHeight;

	return newRectGrid;
}

//--------------------------------------------------------------------------------------------------

Entity* entRectGrid_create(ofxXmlSettings &xml)
{
	return new EntRectGrid(xml);
}

//--------------------------------------------------------------------------------------------------